/**
 * 
 */
package com.novartis.nims.trialsubmitter.views;

import com.novartis.nims.trialsubmitter.ApplicationConfiguration;

import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Point;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JFrame;
import javax.swing.JDialog;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.UnsupportedLookAndFeelException;

import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.BoxLayout;
import java.awt.Font;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.border.EtchedBorder;
import javax.swing.JCheckBox;


/**
 * @author snydejo1
 *
 */
public class AdvancedView extends SwingView {
	
	private JPanel jContentPane = null;
	private JMenuBar jJMenuBar = null;
	private JMenu fileMenu = null;
	private JMenu helpMenu = null;
	private JMenuItem exitMenuItem = null;
	private JMenuItem usageMenuItem = null;
	private JDialog usageDialog = null;
	private JLabel mInputLabel = null;
	private JTextField mInputField = null;
	private JLabel mOutputLabel = null;
	private JTextField mOutputField = null;
	private JButton mExecuteButton = null;
	private JButton mInputBrowseButton = null;
	private JButton mOutputBrowseButton = null;
	private JSpinner mSubmissionSpinner = null;
	private JPanel mInputPane = null;
	private JPanel mButtonPane = null;
	private JPanel mSubmissionPanel = null;
	private JLabel mSubmissionNumberLabel = null;
	private JCheckBox mSortImagesCheckBox = null;
	private JCheckBox mCleanImagesCheckBox = null;
	private JCheckBox mGenerateODMCheckBox = null;
	
	/**
	 * @throws UnsupportedLookAndFeelException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @throws ClassNotFoundException 
	 * 
	 */
	public AdvancedView(ApplicationConfiguration config) {
		super(config);
	}
	
	public String getInputDirectory() {
		return mInputField.getText();
	}
	
	public String getOutputDirectory() {
		return mOutputField.getText();
	}
	
	public boolean getDoSort() {
		return mSortImagesCheckBox.isSelected();
	}
	
	public boolean getDoClean() {
		return mCleanImagesCheckBox.isSelected();
	}
	
	public boolean getDoODM() {
		return mGenerateODMCheckBox.isSelected();
	}
	
	public int getSubmissionNumber() {
		return ((SpinnerNumberModel)mSubmissionSpinner.getModel()).getNumber().intValue();
	}

	/**
	 * This method initializes mInputField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getMInputField() {
		if (mInputField == null) {
			mInputField = new JTextField();
		}
		return mInputField;
	}

	/**
	 * This method initializes mOutputField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getMOutputField() {
		if (mOutputField == null) {
			mOutputField = new JTextField();
		}
		return mOutputField;
	}

	/**
	 * This method initializes mExecuteButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getMExecuteButton() {
		if (mExecuteButton == null) {
			mExecuteButton = new JButton();
			mExecuteButton.setText("Execute");
			mExecuteButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent event) {
					Controller.execute();
				}
			});
		}
		return mExecuteButton;
	}

	/**
	 * This method initializes mInputBrowseButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getMInputBrowseButton() {
		if (mInputBrowseButton == null) {
			mInputBrowseButton = new JButton();
			mInputBrowseButton.setText("Browse");
			mInputBrowseButton.setPreferredSize(new Dimension(78, 26));
			mInputBrowseButton.addActionListener(new JFileChooserActionListener(jFrame, mInputField));
		}
		return mInputBrowseButton;
	}

	/**
	 * This method initializes mOutputBrowseButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getMOutputBrowseButton() {
		if (mOutputBrowseButton == null) {
			mOutputBrowseButton = new JButton();
			mOutputBrowseButton.setText("Browse");
			mOutputBrowseButton.setMnemonic(KeyEvent.VK_UNDEFINED);
			mOutputBrowseButton.setPreferredSize(new Dimension(78, 26));
			mOutputBrowseButton.addActionListener(new JFileChooserActionListener(jFrame, mOutputField));
		}
		return mOutputBrowseButton;
	}

	/**
	 * This method initializes mSubmissionSpinner	
	 * 	
	 * @return javax.swing.JSpinner	
	 */
	private JSpinner getMSubmissionSpinner() {
		if (mSubmissionSpinner == null) {
			mSubmissionSpinner = new JSpinner();
			mSubmissionSpinner.setFont(new Font("Dialog", Font.PLAIN, 12));
			SpinnerModel model = new SpinnerNumberModel(1, 1, 100, 1);
			mSubmissionSpinner.setModel(model);
			
		}
		return mSubmissionSpinner;
	}

	/**
	 * This method initializes mInputPane	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getMInputPane() {
		if (mInputPane == null) {
			GridBagConstraints gridBagConstraints31 = new GridBagConstraints();
			gridBagConstraints31.gridx = 0;
			gridBagConstraints31.anchor = GridBagConstraints.WEST;
			gridBagConstraints31.gridy = 7;
			GridBagConstraints gridBagConstraints22 = new GridBagConstraints();
			gridBagConstraints22.gridx = 0;
			gridBagConstraints22.anchor = GridBagConstraints.WEST;
			gridBagConstraints22.gridy = 6;
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.gridx = 0;
			gridBagConstraints11.anchor = GridBagConstraints.WEST;
			gridBagConstraints11.gridy = 5;
			GridBagConstraints gridBagConstraints51 = new GridBagConstraints();
			gridBagConstraints51.gridx = 0;
			gridBagConstraints51.anchor = GridBagConstraints.WEST;
			gridBagConstraints51.insets = new Insets(0, 0, 0, 0);
			gridBagConstraints51.gridy = 4;
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.insets = new Insets(0, 2, 2, 2);
			gridBagConstraints6.gridy = 3;
			gridBagConstraints6.anchor = GridBagConstraints.WEST;
			gridBagConstraints6.gridx = 1;
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.insets = new Insets(0, 2, 2, 2);
			gridBagConstraints5.gridy = 1;
			gridBagConstraints5.ipadx = 0;
			gridBagConstraints5.ipady = 0;
			gridBagConstraints5.anchor = GridBagConstraints.WEST;
			gridBagConstraints5.gridx = 1;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.fill = GridBagConstraints.BOTH;
			gridBagConstraints3.gridwidth = 1;
			gridBagConstraints3.gridx = 0;
			gridBagConstraints3.gridy = 3;
			gridBagConstraints3.weightx = 1.0;
			gridBagConstraints3.anchor = GridBagConstraints.WEST;
			gridBagConstraints3.insets = new Insets(0, 2, 2, 2);
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.anchor = GridBagConstraints.WEST;
			gridBagConstraints2.gridx = 0;
			gridBagConstraints2.gridy = 2;
			gridBagConstraints2.insets = new Insets(2, 2, 0, 2);
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = GridBagConstraints.BOTH;
			gridBagConstraints1.gridwidth = 1;
			gridBagConstraints1.gridx = 0;
			gridBagConstraints1.gridy = 1;
			gridBagConstraints1.ipadx = 0;
			gridBagConstraints1.ipady = 0;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.anchor = GridBagConstraints.WEST;
			gridBagConstraints1.insets = new Insets(0, 2, 2, 2);
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.anchor = GridBagConstraints.WEST;
			gridBagConstraints.gridx = 0;
			gridBagConstraints.gridy = 0;
			gridBagConstraints.ipadx = 0;
			gridBagConstraints.ipady = 0;
			gridBagConstraints.insets = new Insets(2, 2, 0, 2);
			mInputPane = new JPanel();
			mInputPane.setLayout(new GridBagLayout());
			mInputPane.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
			mInputPane.add(mInputLabel, gridBagConstraints);
			mInputPane.add(getMInputField(), gridBagConstraints1);
			mInputPane.add(mOutputLabel, gridBagConstraints2);
			mInputPane.add(getMOutputField(), gridBagConstraints3);
			mInputPane.add(getMInputBrowseButton(), gridBagConstraints5);
			mInputPane.add(getMOutputBrowseButton(), gridBagConstraints6);
			mInputPane.add(getMSubmissionPanel(), gridBagConstraints51);
			mInputPane.add(getMSortedDirectoryCheckBox(), gridBagConstraints11);
			mInputPane.add(getMCleanImagesCheckBox(), gridBagConstraints22);
			mInputPane.add(getMGenerateODMCheckBox(), gridBagConstraints31);
		}
		return mInputPane;
	}

	/**
	 * This method initializes mButtonPane	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getMButtonPane() {
		if (mButtonPane == null) {
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setAlignment(FlowLayout.RIGHT);
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.fill = GridBagConstraints.BOTH;
			gridBagConstraints4.gridx = 0;
			gridBagConstraints4.gridy = 0;
			gridBagConstraints4.insets = new Insets(2, 2, 0, 2);
			mButtonPane = new JPanel();
			mButtonPane.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
			mButtonPane.setLayout(flowLayout);
			mButtonPane.add(getMExecuteButton(), null);
		}
		return mButtonPane;
	}

	/**
	 * This method initializes mSubmissionPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getMSubmissionPanel() {
		if (mSubmissionPanel == null) {
			FlowLayout flowLayout1 = new FlowLayout();
			flowLayout1.setAlignment(FlowLayout.LEFT);
			flowLayout1.setVgap(2);
			flowLayout1.setHgap(2);
			mSubmissionNumberLabel = new JLabel();
			mSubmissionNumberLabel.setText("Submission number");
			mSubmissionPanel = new JPanel();
			mSubmissionPanel.setLayout(flowLayout1);
			mSubmissionPanel.add(mSubmissionNumberLabel, null);
			mSubmissionPanel.add(getMSubmissionSpinner(), null);
		}
		return mSubmissionPanel;
	}

	/**
	 * This method initializes mSortedDirectoryCheckBox	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getMSortedDirectoryCheckBox() {
		if (mSortImagesCheckBox == null) {
			mSortImagesCheckBox = new JCheckBox();
			mSortImagesCheckBox.setText("Sort images (if unchecked, then images must be pre-sorted)");
			mSortImagesCheckBox.setSelected(true);
		}
		return mSortImagesCheckBox;
	}

	/**
	 * This method initializes mCleanImagesCheckBox	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getMCleanImagesCheckBox() {
		if (mCleanImagesCheckBox == null) {
			mCleanImagesCheckBox = new JCheckBox();
			mCleanImagesCheckBox.setText("Clean images");
			mCleanImagesCheckBox.setSelected(true);
		}
		return mCleanImagesCheckBox;
	}

	/**
	 * This method initializes mGenerateODMCheckBox	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getMGenerateODMCheckBox() {
		if (mGenerateODMCheckBox == null) {
			mGenerateODMCheckBox = new JCheckBox();
			mGenerateODMCheckBox.setSelected(true);
			mGenerateODMCheckBox.setText("Generate ODM for trial submission");
		}
		return mGenerateODMCheckBox;
	}

	/**
	 * This method initializes jFrame
	 * 
	 * @return javax.swing.JFrame
	 */
	public JFrame getJFrame() {
		if (jFrame == null) {
			jFrame = new JFrame();
			jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			jFrame.setJMenuBar(getJJMenuBar());
			jFrame.setSize(521, 289);
			jFrame.setContentPane(getJContentPane());
			jFrame.setTitle("Imaging Trial Submitter");
		}
		return jFrame;
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
			gridBagConstraints21.fill = GridBagConstraints.BOTH;
			gridBagConstraints21.gridy = 4;
			gridBagConstraints21.weightx = 1.0;
			gridBagConstraints21.anchor = GridBagConstraints.WEST;
			gridBagConstraints21.insets = new Insets(2, 2, 2, 2);
			gridBagConstraints21.gridx = 1;
			mOutputLabel = new JLabel();
			mOutputLabel.setText("Location of empty directory to save trial submission");
			mInputLabel = new JLabel();
			mInputLabel.setText("Location of directory containing subject directories");
			mInputLabel.setDisplayedMnemonic(KeyEvent.VK_UNDEFINED);
			jContentPane = new JPanel();
			jContentPane.setLayout(new BoxLayout(getJContentPane(), BoxLayout.Y_AXIS));
			jContentPane.add(getMInputPane(), null);
			jContentPane.add(getMButtonPane(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jJMenuBar	
	 * 	
	 * @return javax.swing.JMenuBar	
	 */
	private JMenuBar getJJMenuBar() {
		if (jJMenuBar == null) {
			jJMenuBar = new JMenuBar();
			jJMenuBar.add(getFileMenu());
			jJMenuBar.add(getHelpMenu());
		}
		return jJMenuBar;
	}

	/**
	 * This method initializes jMenu	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getFileMenu() {
		if (fileMenu == null) {
			fileMenu = new JMenu();
			fileMenu.setText("File");
			fileMenu.add(getExitMenuItem());
		}
		return fileMenu;
	}

	/**
	 * This method initializes jMenu	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getHelpMenu() {
		if (helpMenu == null) {
			helpMenu = new JMenu();
			helpMenu.setText("Help");
			helpMenu.add(getUsageMenuItem());
		}
		return helpMenu;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getExitMenuItem() {
		if (exitMenuItem == null) {
			exitMenuItem = new JMenuItem();
			exitMenuItem.setText("Exit");
			exitMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					System.exit(0);
				}
			});
		}
		return exitMenuItem;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getUsageMenuItem() {
		if (usageMenuItem == null) {
			usageMenuItem = new JMenuItem();
			usageMenuItem.setText("Usage");
			usageMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JDialog usageDialog = getUsageDialog();
					usageDialog.pack();
					Point loc = getJFrame().getLocation();
					loc.translate(20, 20);
					usageDialog.setLocation(loc);
					usageDialog.setVisible(true);
				}
			});
		}
		return usageMenuItem;
	}

	/**
	 * This method initializes aboutDialog	
	 * 	
	 * @return javax.swing.JDialog
	 */
	private JDialog getUsageDialog() {
		if (usageDialog == null) {
			usageDialog = new JDialog(getJFrame(), true);
			usageDialog.setTitle("Usage");
			usageDialog.setContentPane(getUsageContentPane());
		}
		return usageDialog;
	}

}
